<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12" >
    	<button type="button" data-toggle="modal" class="btn btn-primary" data-target="#product-class-detail" data-backdrop="static">添加分类</button>
    	<br><br>
   </div>
	
	<div class="col-sm-12">
	<table id="product-class-table" class="table table-hover table-bordered table-striped table-striped">
		<thead>
			<tr>
				<td>id</td>
				<td style="width:35%">分类名称</td>
				<td>商品数量</td>
				<td>导航栏</td>
				<td>是否显示</td>
				<td>排序</td>
				<td>操作</td>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	</div>
	<br>
</div>

<include file='Index/footer' />
<include file="Product/product-class-detail" />
<script>
$(document).ready(function() {
		loadTable();		
		loadPlupload();
		loadProductClassTree('parent_id_tree', false, true);
});

function loadTable(){
	var table = $('#product-class-table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"order": [[ 0, "asc" ]],
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/Product/getProductClass/")}',
		"columns": [
	        { "data": 'id' },
	        { "data": 'name' },
	        { "data": 'amount' },
	        { "data": 'navbar' },
	        { "data": 'display' },
	        { "data": 'index' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
	    {
	    	"targets": 0,
	      "visible":false
	    },
	    {
	    	"targets": 1,
	    	"render": function (data, type, row, table) {
	    		//console.log(row);
	    		if(row.type == "1")
	        	return data;
	    		else if(row.type == "2")
	        	return '<div class="col-sm-4"></div>—'+data;
	        else if(row.type == "3")
	        	return '<div class="col-sm-8"></div>—'+data;
	      }
	    },
	    {
	    	"targets": [3, 4],
	    	"render": function (data, type, row, table) {
	    		if(data == '1')
	        	return '是';
	        else
	        	return '否';
	      }
	    },
	    {
	    	"targets": -1,
	    	"data": null,
	    	"render": function (data, type, row, table) {
	        return '<button class="btn btn-info" onclick="edit(\''+row.product_class_id+'\')">编辑</button>  <button class="btn btn-danger" onclick="del(\''+row.product_class_id+'\')">移除</button>';
	      },
	    }
    ]
	});
}

var imgurl = '';
function loadPlupload(){
	$("#uploadImg").plupload({
    // General settings
    runtimes : 'html5,flash,silverlight,html4',
    url : '{:U("Admin/Product/uploadProductClass")}',

    // User can upload no more then 20 files in one go (sets multiple_queues to false)
    max_file_count:1,
    chunk_size: '10mb',
    filters : {
        // Maximum file size
        max_file_size : '10mb',
        // Specify what files to browse for
        mime_types: [
            {title : "Image files", extensions : "jpg,gif,png"}
            /* {title : "Zip files", extensions : "zip"}*/
        ]
    },

    // Rename files by clicking on their titles
    rename: false,
    // Sort files
    sortable: true,
    // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
    dragdrop: true,
    //unique_names:true,
    // Views to activate
    views: {
        list: true,
        thumbs: true, // Show thumbs
        active: 'thumbs'
    },
    // Flash settings
    flash_swf_url : '__PUBLIC__/bower_components/plupload/js/Moxie.swf',

    // Silverlight settings
    silverlight_xap_url : '__PUBLIC__/bower_components/plupload/js/Moxie.xap',

    //确定上传事件
    init: {
        /*FileUploaded: function (up, file, result) {
            alert(result.response);
        }*/
        BeforeUpload:function(uploader,file){
					if(imgurl){
						alert("将会覆盖之前上传的分类图片！");
					}
        },
        FileUploaded:function(uploader,file,responseObject){
        	var obj = eval('('+responseObject.response+')'); 
        	imgurl = rootUrl+'/Public/upload/product_class/' + obj.savename;
        	$('#img').html('<img height="100px" src="'+imgurl+'" />');
					//console.log(responseObject);
        },
        UploadComplete:function(up,file){
            //console.log(up);
            //console.log(file);
        }
    }
	});
}

function edit(product_class_id){
	//编辑按钮点击，弹出编辑框
	$.ajax({
			url: '{:U("Admin/Product/getProductClass/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"product_class_id":product_class_id
			}
		})
		.success(function(data){
			//console.log(data);
			var treeObj = $.fn.zTree.getZTreeObj("parent_id_tree");
			var node = treeObj.getNodeByParam("product_class_id", data.parent_id);
			if(data.type == '1'){
				node = treeObj.getNodeByParam("product_class_id", 'a');
			}
			treeObj.selectNode(node);

			$("input[name='product_class_id']").attr('value', data.product_class_id);
			$("input[name='name']").attr('value', data.name);
			$("input[name='index']").attr('value', data.index);
			$("textarea[name='descript']").html(data.descript);
			imgurl = data.imgurl;
			$('#img').html('<img width="150px" src="'+data.imgurl+'" />');

			$('#display'+data.display).attr('checked', 'true');
			$('#navbar'+data.navbar).attr('checked', 'true');

			$('#product-class-detail').modal({
				backdrop : "static"
			});
	});
}

function del(product_class_id){
	if(!confirm("删除该分类下所有子分类以及该分类下所有商品，确认删除该分类吗？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/Product/deleteProductClass/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"product_class_id":product_class_id
			}
		})
		.success(function(data){
			location.reload();
	});
}
</script>
